using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPCoco.DataStrucure;

namespace WPCoco
{
    /// <summary>
    /// Structure that allow a minimum number of SpriteBatch object created, lowering the graphics resouces to spend
    /// </summary>
    public static class SpriteBatchCache
    {
        static List<CachedSpriteBatch> freeList = new List<CachedSpriteBatch>(3);
        static Set<CachedSpriteBatch> usedList = new Set<CachedSpriteBatch>();

        /// <summary>
        /// Retrieve one CachedSpriteBatch (SpriteBatch) object with retain count 1
        /// </summary>
        /// <returns></returns>
        public static CachedSpriteBatch GetVolatile()
        {
            if (freeList.Count == 0)
                freeList.Add(new CachedSpriteBatch(Screen.CurrentGraphicsDevice));
            int index = freeList.Count - 1;
            CachedSpriteBatch spriteBatch = freeList[index];
            freeList.RemoveAt(index);
            usedList.Add(spriteBatch);
            return spriteBatch;
        }

        internal static void Restore(CachedSpriteBatch spriteBatch) 
        {
            if (spriteBatch == null) return;
            freeList.Add(spriteBatch);
            usedList.Remove(spriteBatch);
        }
    }
}
